package com.lan.iot.utils;

import android.content.SharedPreferences;

import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpRequestUtil {
    static SharedPreferences sp;
    private static OkHttpClient client;
    public static String accessToken;
    public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");

    static {
        client = new OkHttpClient();
    }

    public static Response getAccess(String url, String token) throws IOException {

        //创建OkHttpClient对象
        OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(3000, TimeUnit.MILLISECONDS).readTimeout(3000, TimeUnit.MILLISECONDS).build();
        //创建Request
        Request request = new Request.Builder()
                .url(url)//访问连接
                .addHeader("AccessToken", token)
                .get()
                .build();
        //创建Call对象
        Call call = client.newCall(request);
        //通过execute()方法获得请求响应的Response对象
        Response response = call.execute();
        return response;
    }

    ;   public static Response getAccess(String url, String token, Map<String,String> urlVals) throws IOException {
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(url);
        stringBuilder.append("?");
        Set<String> keys = urlVals.keySet();
        for (String key : keys) {
            stringBuilder.append(key+"=");
            stringBuilder.append(urlVals.get(key));
            stringBuilder.append("&");
        }

        url=stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1);
        //创建OkHttpClient对象
        OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(3000, TimeUnit.MILLISECONDS).readTimeout(3000, TimeUnit.MILLISECONDS).build();
        //创建Request
        Request request = new Request.Builder()
                .url(url)//访问连接
                .addHeader("AccessToken", token)
                .get()
                .build();
        //创建Call对象
        Call call = client.newCall(request);
        //通过execute()方法获得请求响应的Response对象
        Response response = call.execute();
        return response;
    }

    public static Response postAccess(String url, String json) throws IOException {
        String token = sxToken();
        if ("".equals(token)){
            return null;
        }
        OkHttpClient client = null;
        Request request = null;
        Response execute = null;
        client = null;
        request = null;
        final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        //创建okhttp对象
        RequestBody body = RequestBody.create(JSON, json);
        //创建OkHttpClient对象
        client = new OkHttpClient().newBuilder().connectTimeout(3000, TimeUnit.MILLISECONDS).readTimeout(3000, TimeUnit.MILLISECONDS).build();

        //创建Request
        request = new Request.Builder()
                .url(url)//访问连接
                .addHeader("AccessToken", token)
                .post(body)
                .build();
        execute = client.newCall(request).execute();
        return execute;
    };

    public static Response postAccess(String url,Map<String,String> urlVals,String bodyStr,String token) throws IOException {
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(url);
        stringBuilder.append("?");
        Set<String> keys = urlVals.keySet();
        for (String key : keys) {
            stringBuilder.append(key+"=");
            stringBuilder.append(urlVals.get(key));
            stringBuilder.append("&");
        }

        url=stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1);

        if ("".equals(token)){
            return null;
        }
        OkHttpClient client = null;
        Request request = null;
        Response execute = null;
        client = null;
        request = null;
        final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        //创建okhttp对象
        RequestBody body = RequestBody.create(JSON, bodyStr);
        //创建OkHttpClient对象
        client = new OkHttpClient().newBuilder().connectTimeout(3000, TimeUnit.MILLISECONDS).readTimeout(3000, TimeUnit.MILLISECONDS).build();

        //创建Request
        request = new Request.Builder()
                .url(url)//访问连接
                .addHeader("AccessToken", token)
                .post(body)
                .build();
        execute = client.newCall(request).execute();
        return execute;
    };

    public static Response deleteAccess(String url, String json) throws IOException {
        String token = sxToken();
        if ("".equals(token)){
            return null;
        }
        final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        //创建okhttp对象
        RequestBody body = RequestBody.create(JSON, json);
        //创建OkHttpClient对象
        OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(3000, TimeUnit.MILLISECONDS).readTimeout(3000, TimeUnit.MILLISECONDS).build();
        //创建Request
        Request request = new Request.Builder()
                .url(url)//访问连接
                .addHeader("AccessToken", token)
                .delete(body)
                .build();
        return client.newCall(request).execute();
    }

    ;

    private static String sxToken() {
        accessToken = sp.getString("accessToken", "");
        if ("".equals(accessToken)) {
            try {
                for (int i = 0; i < 3; i++) {
                    Thread.sleep(150);
                    accessToken = sp.getString("accessToken", "");
                    if (!"".equals(accessToken)) {
                        continue;
                    }
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return accessToken;
    }


}
